/*
the controls are 
w for up
s for down
d for right
a for left 
j to reset the level
the colored things are portals
there are 3 keys distributed among the map which you need to find to reach Hamada

@title: tung sahur's waking hamada
@author: 3bdo19 
@description: None
@tags: []
@addedOn: 2025-05-13
*/

const player = "p";
const night = "n";
const night_with_key = "u";
const house_walls = "h";
const inside_house = "i";
const inside_house_with_stairs = "s";
const portal_green = "g";
const portal_red = "r";
const bell = "b";
const wall_with_key = "e";
const lock = "l";
const reverse_bell = "o";
const portal_brown = "t";
const portal_blue = "k";
const lock_2 = "m";
const hamada = "x";
const wall_key = "z";
const portal_yellow = "c";
const portal_orange = "q";
const hamada_lock = "f";
const portal_pink = "y";
const portal_purple = "2";
const melody = tune`
105.63380281690141,
105.63380281690141: F4/105.63380281690141,
105.63380281690141: F4/105.63380281690141 + G4/105.63380281690141 + F5~105.63380281690141,
105.63380281690141: B4-105.63380281690141 + G4-105.63380281690141 + F4-105.63380281690141,
105.63380281690141: G4/105.63380281690141 + E5-105.63380281690141,
105.63380281690141: G4/105.63380281690141 + D4-105.63380281690141 + G5~105.63380281690141 + A4~105.63380281690141,
105.63380281690141: G4/105.63380281690141,
105.63380281690141: C5^105.63380281690141 + G4/105.63380281690141 + E5^105.63380281690141,
105.63380281690141: F4/105.63380281690141 + G5-105.63380281690141,
105.63380281690141: A4^105.63380281690141 + C5^105.63380281690141 + E4/105.63380281690141,
105.63380281690141: D4~105.63380281690141 + E5~105.63380281690141 + A5^105.63380281690141,
105.63380281690141: C5^105.63380281690141 + A4^105.63380281690141 + F4/105.63380281690141 + G5-105.63380281690141,
105.63380281690141: A4/105.63380281690141,
105.63380281690141: C5/105.63380281690141 + A4/105.63380281690141 + G4/105.63380281690141 + D4-105.63380281690141 + F5~105.63380281690141,
105.63380281690141: B4/105.63380281690141 + C4/105.63380281690141,
105.63380281690141: G4/105.63380281690141 + F4-105.63380281690141 + G5-105.63380281690141 + C5~105.63380281690141,
105.63380281690141: F5~105.63380281690141,
105.63380281690141: A4^105.63380281690141 + G4-105.63380281690141 + D5-105.63380281690141 + B4~105.63380281690141,
105.63380281690141: E5^105.63380281690141 + E4/105.63380281690141 + B5-105.63380281690141 + G5~105.63380281690141,
105.63380281690141: B4^105.63380281690141 + D4^105.63380281690141,
105.63380281690141: B4^105.63380281690141 + F4/105.63380281690141 + A5-105.63380281690141 + E4^105.63380281690141,
105.63380281690141,
105.63380281690141: C5^105.63380281690141 + F5-105.63380281690141 + G4/105.63380281690141 + B4/105.63380281690141 + D4~105.63380281690141,
105.63380281690141: A5/105.63380281690141 + B4/105.63380281690141 + F5^105.63380281690141,
105.63380281690141: E5^105.63380281690141,
105.63380281690141: G4^105.63380281690141 + C5/105.63380281690141 + F5-105.63380281690141 + G5-105.63380281690141 + E4~105.63380281690141,
105.63380281690141,
105.63380281690141: F5/105.63380281690141 + A4/105.63380281690141 + D5/105.63380281690141,
105.63380281690141: B4^105.63380281690141 + F4/105.63380281690141,
105.63380281690141: F4/105.63380281690141 + C4/105.63380281690141,
105.63380281690141: F5/105.63380281690141 + D4/105.63380281690141,
105.63380281690141: G5/105.63380281690141 + F4/105.63380281690141`

setLegend(
  [ player, bitmap`
................
......99999.....
......90909.....
......99999.....
.....99.9.99....
......99.99.....
.....9999999....
....999999999...
....0.99999.9...
...0..99999.....
..0...99999.....
.0....99999.....
......9...9.....
......99..99....
......9...9.....
.....99..99.....`],
  [ night, bitmap`
0000000000000000
0000020000200000
0000000000002000
0020200020000000
0000000000000000
0000000000000000
0000000000000000
0020000000000020
0000000200000000
0000000000000000
0000000000000020
0000000000000000
0002000000020000
0000000000000000
0000000000000000
0000000000000000` ],
  [ house_walls, bitmap`
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC` ],
  [ inside_house, bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL` ],
  [ inside_house_with_stairs, bitmap`
LLLLCLLLLLLCLLLL
LLLLCLLLLLLCLLLL
LLLLCCCCCCCCLLLL
LLLLCLLLLLLCLLLL
LLLLCLLLLLLCLLLL
LLLLCCCCCCCCLLLL
LLLLCLLLLLLCLLLL
LLLLCLLLLLLCLLLL
LLLLCCCCCCCCLLLL
LLLLCLLLLLLCLLLL
LLLLCLLLLLLCLLLL
LLLLCCCCCCCCLLLL
LLLLCLLLLLLCLLLL
LLLLCLLLLLLCLLLL
LLLLCCCCCCCCLLLL
LLLLCCCCCCCCLLLL` ],
  [ portal_green, bitmap`
LLLLLLLLLLLLLLLL
LLL4444424444LLL
LLL44444444444LL
LLL44444444444LL
LLL44442422244LL
LL444442442444LL
LL442444422444LL
LL442244424444LL
LL444224444444LL
LL44422444244LLL
LL44444424444LLL
LL44444424444LLL
LL444244224.4LLL
LL44444442444LLL
LLL444444444LLLL
LLLLLLLLLLLLLLLL` ],
  [ portal_red, bitmap`
LLLLLLLLLLLLLLLL
LLL3333323333LLL
LLL33333333333LL
LLL33333333333LL
LLL33332322233LL
LL333332332333LL
LL332333322333LL
LL332233323333LL
LL333223333333LL
LL33322333233LLL
LL33333323333LLL
LL33333323333LLL
LL333233223.3LLL
LL33333332333LLL
LLL333333333LLLL
LLLLLLLLLLLLLLLL` ],  
  [ bell, bitmap`
LLLLLLLLLLLLLLLL
LLLLLLL66LLLLLLL
LLLLLL6666LLLLLL
LLLLLL6666LLLLLL
LLLLL666666LLLLL
LLLL66666666LLLL
LLL6666666666LLL
LLL6666666666LLL
LL666666666666LL
LL666666666666LL
LL666666666666LL
LL666666666666LL
LLLLLLL66LLLLLLL
LLLLLLL66LLLLLLL
LLLLLLL66LLLLLLL
LLLLLLLLLLLLLLLL` ],
  [ wall_with_key, bitmap`
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCC000CCCCCCCCCC
CCC000CCCCCCCCCC
CCCC0CCCCCCCCCCC
CCCC00CCCCCCCCCC
CCCC00CCCCCCCCCC
CCC00CCCCCCCCCCC` ],
  [ night_with_key, bitmap`
0000000000000000
0000020000200000
0000000000002000
0020200020000000
0000000000000000
0200000000000000
0002000000200000
0000000000200002
0000000000000000
0222200000000000
0200200000000000
0222200000000000
0002000000200000
0002220002200000
0222000000000000
0222000000000000` ],
  [ lock, bitmap`
3000000990000003
3330000990000333
0033000990003300
0003300990033000
0000330990330000
0000033993300000
0000033333300000
9999939999399999
9999939999399999
0000033333300000
0000033993300000
0000330990330000
0003300990033000
0033000990003300
3330000990000333
3000000990000003` ],
  [ reverse_bell, bitmap`
LLLLLLLLLLLLLLLL
LLLLLLL66LLLLLLL
LLLLLLL66LLLLLLL
LLLLLLL66LLLLLLL
LL666666666666LL
LL666666666666LL
LL666666666666LL
LL666666666666LL
LLL6666666666LLL
LLL6666666666LLL
LLLL66666666LLLL
LLLLL666666LLLLL
LLLLLL6666LLLLLL
LLLLLL6666LLLLLL
LLLLLLL66LLLLLLL
LLLLLLLLLLLLLLLL` ],
  [ portal_brown, bitmap`
LLLLLLLLLLLLLLLL
LLLLCCCCCCCLLLLL
LLLLCCC2CCCCCLLL
LLLLCCC2CCCCCLLL
LLLCC2C2CCCCCLLL
LLLCCCCCCC2CCCLL
LLLCCCCCCC2CCCLL
LLCC22CC2CCCCCLL
LLCCC2C22CCC2CLL
LLCCCCCCCCCCCCLL
LLCCCCCCC2CCCCLL
LLCC2CCC22CCCCLL
LLLCCCCCCCCCCCLL
LLLLCC2CCCC2CLLL
LLLLLCCCCCCCLLLL
LLLLLLLLLLLLLLLL` ],
  [ portal_blue, bitmap`
LLLLLLLLLLLLLLLL
LLLL7777777LLLLL
LLLL777277777LLL
LLLL777277777LLL
LLL7727277777LLL
LLL77777772777LL
LLL77777772777LL
LL772277277777LL
LL777272277727LL
LL777777777777LL
LL777777727777LL
LL772777227777LL
LLL77777777777LL
LLLL772777727LLL
LLLLL7777777LLLL
LLLLLLLLLLLLLLLL` ],  
  [ lock_2, bitmap`
6HHHHH6HH6HHHHH6
66HHHH6666HHHH66
H66HHHH66HHHH66H
HH66HHH66HHH66HH
HHH666H66H666HHH
HHHHH666666HHHHH
HHHHHHH66HHHHHHH
HHHH666666666HHH
66666HH66HH66666
HHHHHHH66HHHHHHH
HHHHH666666HHHHH
HHHH66H66H66HHHH
HHH66HH66HH66HHH
HH66HHH66HHH66HH
H66HHH6666HHH66H
66HHHH6HH6HHHH66` ],
  [ wall_key, bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLL666666LLLLL
LLLLL6LLLL6LLLLL
LLLLL666666LLLLL
LLLLLLL66LLLLLLL
LLLLLLL66LLLLLLL
LLLLLLL6666LLLLL
LLLLLLL66LLLLLLL
LLLLLLL6666LLLLL
LLLLL6666LLLLLLL
LLLLL6666LLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL` ],
  [ hamada, bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLL0220LLLLLL
LLLLLL2222LLLLLL
LLLLLL2002LLLLLL
LLLLLLL22LLLLLLL
LLLLLLL22LLLLLLL
LLLLL222222LLLLL
LLLL22L22L22LLLL
LLLL2LL22LL2LLLL
LLLL2LL22LL2LLLL
LLLLLLL22LLLLLLL
LLLLLLL22LLLLLLL
LLLLLL2222LLLLLL
LLLLL22LL22LLLLL
LLLLL2LLLL2LLLLL` ],
  [ portal_yellow, bitmap`
LLLLLLLLLLLLLLLL
LLL6666626666LLL
LLL66666666666LL
LLL66666666666LL
LLL66662622266LL
LL666662662666LL
LL662666622666LL
LL662266626666LL
LL666226666666LL
LL66622666266LLL
LL66666626666LLL
LL66666626666LLL
LL666266226.6LLL
LL66666662666LLL
LLL666666666LLLL
LLLLLLLLLLLLLLLL` ],
  [ portal_orange, bitmap`
LLLLLLLLLLLLLLLL
LLL9999929999LLL
LLL99999999999LL
LLL99999999999LL
LLL99992922299LL
LL999992992999LL
LL992999922999LL
LL992299929999LL
LL999229999999LL
LL99922999299LLL
LL99999929999LLL
LL99999929999LLL
LL999299229.9LLL
LL99999992999LLL
LLL999999999LLLL
LLLLLLLLLLLLLLLL` ],
  [ hamada_lock, bitmap`
5588888888888555
8555888888885588
8885588888855888
8888558888558888
8888855885588888
8888885555888888
8888888558888888
8888888558888888
8888888558888888
8888885555888888
8888855885588888
8888558888558888
8885588888855888
8855888888885588
8558888888888558
5588888888888855` ],
  [ portal_pink, bitmap`
LLLLLLLLLLLLLLLL
LLLL8888888LLLLL
LLLL888288888LLL
LLLL888288888LLL
LLL8828288888LLL
LLL88888882888LL
LLL88888882888LL
LL882288288888LL
LL888282288828LL
LL888888888888LL
LL888888828888LL
LL882888228888LL
LLL88888888888LL
LLLL882888828LLL
LLLLL8888888LLLL
LLLLLLLLLLLLLLLL` ],  
  [ portal_purple, bitmap`
LLLLLLLLLLLLLLLL
LLLLHHHHHHHLLLLL
LLLLHHH2HHHHHLLL
LLLLHHH2HHHHHLLL
LLLHH2H2HHHHHLLL
LLLHHHHHHH2HHHLL
LLLHHHHHHH2HHHLL
LLHH22HH2HHHHHLL
LLHHH2H22HHH2HLL
LLHHHHHHHHHHHHLL
LLHHHHHHH2HHHHLL
LLHH2HHH22HHHHLL
LLLHHHHHHHHHHHLL
LLLLHH2HHHH2HLLL
LLLLLHHHHHHHLLLL
LLLLLLLLLLLLLLLL` ],    
)

setSolids([player, house_walls, wall_key, lock, lock_2, hamada_lock])

let level = 0
const levels = [
  map`
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
iiiiiiiiiiiiiiiiiiii
pnnnnnnnnnnnnnnnnnnx`,
  map`
nnnnnnhhnnnnnn
nnnnnhiihnnnnn
nnnnnhizhnnnnn
nnnnhiihyhnnnn
nnuhtmihi2hnnn
nnhohhshhhxhnn
nnhiiishhkfhnn
nnhshhhhhhhhnn
nnhsliiiiibhrn
nnhslibibiihnn
nnhbhhhhhhshnn
nnhehhhhqishnn
nnpiiiiighhhnc`
]

setMap(levels[level])
  clearText()

addText("welcome to the", { y: 2, color: color`7` });
addText("tum tum tum", { x: 5, y: 4, color: color`7` });
addText("sahur", { x: 8, y: 6, color: color`7` });
addText("you need to wake", { y: 10, color: color`7` });
addText("hamada to eat", {x: 4, y: 12, color: color`7` });
addText("his sahur", { y: 14, color: color`7` });


onInput("a", () => {
	getFirst(player).x -= 1

})

onInput("d", () => {
	getFirst(player).x += 1

})

onInput("w", () => {
	getFirst(player).y -= 1

})

onInput("s", () => {
	getFirst(player).y += 1

})

onInput("j", () => {
    setMap(levels[level])

});



afterInput(() => {
  const greenportalscovered = tilesWith(player, portal_green);
  const yellowportalscovered = tilesWith(player, portal_yellow);
  const redportalscovered = tilesWith(player, portal_red);
  const orangeportalscovered = tilesWith(player, portal_orange);
  const brownportalscovered = tilesWith(player, portal_brown);
  const pinkportalscovered = tilesWith(player, portal_pink);
  const blueportalscovered = tilesWith(player, portal_blue);
  const purpleportalscovered = tilesWith(player, portal_purple);
  const hamadacovered = tilesWith(player, hamada); // tiles that both contain the tum-tum-tum-sahur and hamada
  const wallkeysTaken = tilesWith(player, wall_with_key); // ADDED: all the keys that the player is on
  const yellowkeysTaken = tilesWith(player, reverse_bell); // ADDED: all the keys that the player is on
  const nightkeysTaken = tilesWith(player, night_with_key); // ADDED: all the keys that the player is on  
  const trapsCovered = tilesWith(player, bell); // ADDED: tiles with players on traps
  
  // ADDED: teleport the player to the yellow portal if they are standing on the green one
  if (greenportalscovered.length >= 1) {
    const yp = getFirst(portal_yellow);
    const pl = getFirst(player);
    // teleport player to red portal
    pl.x = yp.x;
    pl.y = yp.y;
  } 

  // ADDED: teleport the player to the orange portal if they are standing on the red one
  if (redportalscovered.length >= 1) {
    const op = getFirst(portal_orange);
    const pl = getFirst(player);
    // teleport player to red portal
    pl.x = op.x;
    pl.y = op.y;
  }
  
  // ADDED: teleport the player to the pink portal if they are standing on the brown one
  if (brownportalscovered.length >= 1) {
    const pp = getFirst(portal_pink);
    const pl = getFirst(player);
    // teleport player to pink portal
    pl.x = pp.x;
    pl.y = pp.y;
  }
  
  // ADDED: teleport the player to the orange portal if they are standing on the red one
  if (purpleportalscovered.length >= 1) {
    const bp = getFirst(portal_blue);
    const pl = getFirst(player);
    // teleport player to red portal
    pl.x = bp.x;
    pl.y = bp.y;
  }
  
    // if at least one hamada is overlapping with a sahur, proceed to the next level
    if (hamadacovered.length >= 1) {
        // increase the current level number
        level = level + 1;
        clearText()
      const playback = playTune(melody, Infinity)
        // check if current level number is valid
        if (level < levels.length) {
            setMap(levels[level]);
        } else {
            
            addText("you win!", { y: 4, color: color`7` });
        }
    }

        if (wallkeysTaken.length >= 1) {
        getFirst(lock).remove();
        getFirst(lock).remove();  
        }  

          if (yellowkeysTaken.length >= 1) {
        getFirst(hamada_lock).remove();  
        }  
          if (nightkeysTaken.length >= 1) {
        getFirst(lock_2).remove();
        }  
  
      if (trapsCovered.length > 0) {
        setMap(levels[level])
        }

});  
  